﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using mshtml;
using SHDocVw;

namespace UTTestProj
{
    public   class UTClass
    {

        private static AutoResetEvent DocCompleteEvent = new AutoResetEvent (false );

        /// <summary>
        /// Static function to open IExplore.exe
        /// by using Process
        /// </summary>
        public static void OpenIE()
        {
            Console.WriteLine("Opening IExplore.exe");
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "iexplore.exe";
            startInfo.UseShellExecute = false; 

            // if it's IE8 ,the argument will be added -nomerge
            // others ,there is no necessary to add -nomerge
            Process p = Process.Start("iexplore.exe", "-nomerge about:blank");
            //Process p = Process.Start("iexplore.exe", " about:blank");
            // Process sleep to  wait IE excute completely;
            Thread.Sleep(3000);

            if (!p.HasExited)
            {
                p.Refresh();
            }
            
            Console.WriteLine("attach to IE...");

            InternetExplorer ie=null ;
            if (p!=null)
            { 
                SHDocVw .ShellWindows allWindows = new ShellWindows ();
                for (int i = 1; i < allWindows.Count; i++)
                {                    
                    InternetExplorer iEx = (InternetExplorer)allWindows.Item(i);
                    if ( iEx!=null &&   iEx.HWND == p.MainWindowHandle.ToInt32 ())
                    {
                        ie = iEx;
                        break;
                    }
                }
                if (allWindows.Count <= 0)
                {
                    Console.WriteLine("Error:IE can't lauch!");
                    return ;
                }

            }
            if (ie == null)
            {
                Console.WriteLine("Error:can't attach to IE!");
                return; 
            }

            Console.WriteLine("Navigating....");

            ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
            ie.WindowClosing += new DWebBrowserEvents2_WindowClosingEventHandler(ie_WindowClosing);


            object o = null;
            ie.Navigate("http://www.google.com", ref o, ref o, ref o, ref o);

            DocCompleteEvent.WaitOne();

            mshtml.HTMLDocument htmlDoc = ie.Document as mshtml.HTMLDocument;
            HTMLInputTextElement inputTextEle = htmlDoc.getElementsByName("q").item("q", 0) as HTMLInputTextElement;
            HTMLFormElement inputSubEle = htmlDoc.getElementsByName("f").item("f", 0) as HTMLFormElement;
            if (inputTextEle != null)
            {
                inputTextEle.value = "This is a test!";

                Thread.Sleep(3000);
                inputSubEle.submit();
            }
            //throw new NotImplementedException(); 
        }

        static void ie_WindowClosing(bool IsChildWindow, ref bool Cancel)
        {
            //Process.GetCurrentProcess().Kill();
            //throw new NotImplementedException();
        }

        static void ie_DocumentComplete(object pDisp, ref object URL)
        {
            DocCompleteEvent.Set(); 
        }
    }
}
